%                    Read Drag Data
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Written for: AIAA Student Org
%
%             Do not use without explicit permission from
%             either the author or organization this program
%             was written for. Please cite the author for
%             any code/ideas/thought that was borrowed from
%             this work.
%
% Created by:  David Gitz
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

%Global Variables
clear all;
close all;
DragFileDir = 'DATA\DRAG\';
LiftFileDir = 'DATA\LIFT\';
airfoilString   = 'Airfoil: ';
builderString   = 'Builder: ';
commentString   = 'Comment: ';
numberReString  = 'Number of Reynolds #';
dragreynoldsString  = 'Average Reynolds #:';
draganglesString    = 'Number of angles of attack:';
liftreynoldsString  = 'Average Reynolds #:';
liftanglesString    = 'Number of angles of attack:';
dataStartString = ' alpha';

DragFiles = dir(DragFileDir);
LiftFiles = dir(LiftFileDir);

whichFoil = 1;
whichFile = 1;
whichRe = 1;
whichAngle = 1;
ReCount = 1;
linenum = 1;
AngleCount = 0;
%Initial Data Collection
DragFiles(1) = [];
DragFiles(1) = [];
LiftFiles(1) = [];
LiftFiles(1) = [];
%Read Drag Files

for whichFile = 1:length(DragFiles)
    whichRe = 0;
    AngleCount = 0;
    AirFoils(whichFoil).name = DragFiles(whichFile).name;
    str = [DragFileDir DragFiles(whichFile).name];
    fileID = fopen(str);
    while(~feof(fileID))
        
        line = fgetl(fileID);
        linenum = linenum + 1;
        %checks
        if (strfind(line,builderString))
            AirFoils(whichFoil).builder = line;
        elseif (strfind(line,commentString))
            AirFoils(whichFoil).comment = line;
        elseif (strfind(line,numberReString))
            line = fgetl(fileID);
            linenum = linenum + 1;
            AirFoils(whichFoil).DragReCount = str2num(line);
            ReCount = str2num(line);
        elseif (strfind(line,dragreynoldsString)) %New RE Number
            whichRe = whichRe + 1;
            line = fgetl(fileID);
            linenum = linenum + 1;
            AirFoils(whichFoil).DragData(whichRe).Re = str2num(line);
            %AirFoils(whichFoil).Re(whichRe) = str2num(line);
        elseif (strfind(line,draganglesString))
            line = fgetl(fileID);
            linenum = linenum + 1;
            AngleCount = str2num(line);
        elseif( strfind(line,dataStartString))
            for whichAngle = 1:AngleCount
                line = fgetl(fileID);
                linenum = linenum + 1;
                tempData = str2num(line); %store in a temporary array
                AirFoils(whichFoil).DragData(whichRe).alpha(whichAngle) = tempData(1);
                AirFoils(whichFoil).DragData(whichRe).Cl(whichAngle) = tempData(2);
                AirFoils(whichFoil).DragData(whichRe).Cd(whichAngle) = tempData(3);
                AirFoils(whichFoil).DragData(whichRe).Cspan1(whichAngle) = tempData(4);
                AirFoils(whichFoil).DragData(whichRe).Cspan2(whichAngle) = tempData(5);
                AirFoils(whichFoil).DragData(whichRe).Cspan3(whichAngle) = tempData(6);
                AirFoils(whichFoil).DragData(whichRe).Cspan4(whichAngle) = tempData(7);
            end
            
        end
    end
    fclose(fileID);
    whichFoil = whichFoil + 1;
end

%Read Lift Files
whichFoil = 1;
whichFile = 1;
whichRe = 1;
whichAngle = 1;
ReCount = 1;
linenum = 1;
AngleCount = 0;

for whichFile = 1:length(LiftFiles)
    whichRe = 0;
    AngleCount = 0;
    str = [LiftFileDir LiftFiles(whichFile).name];
    %Find correct AirFoil
    for whichFoil = 1:length(AirFoils)
        if strcmp(LiftFiles(whichFile).name,AirFoils(whichFoil).name)
            if (strcmp(LiftFiles(whichFile).name,'A18T.txt'))
            end
            break;
        end
    end
    fileID = fopen(str);
    while(~feof(fileID))
        
        line = fgetl(fileID);
        linenum = linenum + 1;
        %checks
        if (strfind(line,numberReString))
            line = fgetl(fileID);
            linenum = linenum + 1;
            AirFoils(whichFoil).LiftReCount = str2num(line);
            ReCount = str2num(line);
        elseif (strfind(line,liftreynoldsString)) %New RE Number
            whichRe = whichRe + 1;
            line = fgetl(fileID);
            linenum = linenum + 1;
            AirFoils(whichFoil).LiftData(whichRe).Re = str2num(line);
            %AirFoils(whichFoil).Re(whichRe) = str2num(line);
        elseif (strfind(line,liftanglesString))
            line = fgetl(fileID);
            linenum = linenum + 1;
            AngleCount = str2num(line);
        elseif( strfind(line,dataStartString))
            for whichAngle = 1:AngleCount
                line = fgetl(fileID);
                linenum = linenum + 1;
                tempData = str2num(line); %store in a temporary array
                AirFoils(whichFoil).LiftData(whichRe).alpha(whichAngle) = tempData(1);
                AirFoils(whichFoil).LiftData(whichRe).Cl(whichAngle) = tempData(2);
                AirFoils(whichFoil).LiftData(whichRe).Cm(whichAngle) = tempData(3);
            end
            
        end
    end
    fclose(fileID);
end


% function [ airfoil ] = readDragData( dragFile)
% %readData give it a file name, it returns an 'airfoil' structure
% %   reads the data file and stores it into a structure
% %   Structure looks like:
% %
% %   airfoil(i)         there are as many airfoils as there are names
% %       |
% %       |------name
% %       |
% %       |------builder
% %       |
% %       |------comment
% %       |
% %       |------dragData(j)             there are as many data sets as there are
% %                |                           reynolds numbers
% %                |-----Re
% %                |
% %                |---------alpha(1)----alpha(2)----...---alpha(m)
% %                |
% %                |---------Cl(1)-------Cl(2)-------...---Cl(m)
% %                |
% %                |---------Cd(1)-------Cd(2)-------...---Cd(m)
% %                |
% %                |---------SpanwiseCd(1)..............---SpanwiseCd(m)
% %       |------liftData(k)             there are as many data sets as there are
% %                |                           reynolds numbers
% %                |-----Re
% %                |
% %                |---------alpha(1)----alpha(2)----...---alpha(n)
% %                |
% %                |---------Cl(1)-------Cl(2)-------...---Cl(n)
% %                |
% %                |---------Cm(1)-------Cm(2)-------...---Cm(n)
% %
% %
%
%
% fileID = fopen(dragFile); %open the drag file passed to the function
%
% %These are our known data identifiers. They tell us what information is
% %contained within the data file
% airfoilString   = 'Airfoil: ';
% builderString   = 'Builder: ';
% commentString   = 'Comment: ';
% numberReString  = 'Number of Reynolds #';
% dragreynoldsString  = 'Average Reynolds #:';
% draganglesString    = 'Number of angles of attack:';
% dataStartString = ' alpha /';
% airfoil = airfoils; %create the object
%
% %counters
% i = 0;   %which airfoil you are on
%
% while ~feof(fileID) %while not the end of the file
%     line = fgetl(fileID);
%
%     %find the name of the airfoil and store it to structure
%     if(strfind(line,airfoilString) == 1)
%         i=i+1; %if we found a new name, it means we found a new airfoil
%         airfoil(i).name = strrep(line,airfoilString,'');
%     end
%
%     %find the builder of airfoil and store it to structure
%     if(strfind(line,builderString) == 1)
%         airfoil(i).builder = strrep(line,builderString,'');
%     end
%
%     %find the comment of airfoil and store it to structure
%     if(strfind(line,commentString) == 1)
%         airfoil(i).comment = strrep(line,commentString,'');
%     end
%
%     %find the number of Reynolds Numbers provided, this will be used as a
%     %counter for our loop
%     if(strfind(line,numberReString) == 1)
%         nextLine = fgetl(fileID); %the nextLine contains the actual data
%         numRe = str2num(nextLine);
%
%         countRe = 0; %set counter at zero
%
%         while countRe < numRe
%             line = fgetl(fileID); %read the next line
%             %get the average Re
%             if(strfind(line,dragreynoldsString) == 1)
%                 countRe = countRe +1; %increment the counter because we found new reynolds
%                 nextLine = fgetl(fileID); %Re is the nextLine
%                 airfoil(i).dragData(countRe).Re = str2double(nextLine); %the count is the index of the reynolds number relative to the airfoil it was tested for
%             end
%
%             if(countRe == numRe) %this is to alleviate a data reading error
%                 line=fgetl(fileID);
%             end
%
%             %get the number of angles of atack:
%             if(strfind(line,draganglesString) == 1)
%                 nextLine = fgetl(fileID); %its the next line
%                 numAng =   str2double(nextLine); %get the value
%             end
%
%             if(countRe == numRe) %this is to alleviate a data reading error
%                 line=fgetl(fileID);
%             end
%
%             angCount = 0;
%             if(strfind(line,dataStartString)==1)
%                 %nextLine = fgetl(fileID);
%                 while angCount < numAng %so now we loop over the number of attack angles
%                     angCount = angCount +1;
%                     tempData = fgetl(fileID);
%                     tempData = str2num(tempData); %store in a temporary array
%
%                     airfoil(i).dragData(countRe).alpha(angCount) = tempData(1); %store to structure
%                     airfoil(i).dragData(countRe).Cl(angCount) = tempData(2);
%                     airfoil(i).dragData(countRe).Cd(angCount) = tempData(3);
%                     airfoil(i).dragData(countRe).Cspan1(angCount) = tempData(4);
%                     airfoil(i).dragData(countRe).Cspan2(angCount) = tempData(5);
%                     airfoil(i).dragData(countRe).Cspan3(angCount) = tempData(6);
%                     airfoil(i).dragData(countRe).Cspan4(angCount) = tempData(7);
%                 end
%             end
%         end
%     end
% end
%
%
